---
--类名: WorldBattleView.lua
--创建日期: 2018-9-6
--创建人: 宋建邦
--功能描述:领地争夺战地图
--修改内容:
--修改日期:
--修改人:
--修改备注:
local BattleIconRender = import(".renders.BattleIconRender")

local WorldBattleView = class("WorldBattleView", app.mvc.ViewBase)
WorldBattleView.VIEW_ROOT_RES = Res.BattleWorldMapScene

function WorldBattleView:init()
    self:setBackGroundColorType(0)
    self:setNodeEventEnabled(true)
    self.worldMapModel = app:getInst("WorldMapModel")

    self:initIcons()

    self.btnWorldMap = self._root:findChild("Panel/selected_btn_map")
    self.btnWorldMap:addClickEventListener(function(sender)
        app:sendMsg("WorldBattleController", "closeView" )
        app:sendMsg("WorldBattleController", "clearView" )
        
        app:sendMsg("WorldMapController", "openView" )
    end)

    self.btnBattleMap = self._root:findChild("Panel/selected_btn_battle")

    self.btnBattleMap:setBright(false)
    -- display.setBtnTouchEnable(self.btnBattleMap, false)

    self._root:findChild("Panel/Button_close"):addClickEventListener(function(sender)        
        app:sendMsg("WorldBattleController", "closeView" )
        app:sendMsg("WorldBattleController", "clearView" )
    end)

    self.bc = app:getInst("BiddingController")
    self.evthandler = EM:on(self.bc.townlistChange, function()
        if self.bc.battleMapModel then
            self:refresh(self.bc.battleMapModel)
        end
    end)

    self.btnLook = self._root:findChild("Panel/btn_look")
    self.btnLook:hide()

    self.btnLook:addClickEventListener(function(sender)        
        app:sendMsg("NewguildController", "openView", {tabIdx = 3})
        -- app:sendMsg("WorldBattleController", "closeView" )
        -- app:sendMsg("WorldBattleController", "clearView" )
    end)    

    self.taisaruimu = self._root:findChild("Panel/Panel_tlt/tlt")
    self.year = self._root:findChild("Panel/Panel_tlt/time")

    self.taisaruimu:setString(L("lua_code_text_377"))
end

function WorldBattleView:onExit()
    EM:remove(self.evthandler)
end

function WorldBattleView:initIcons()
    self.icons = {}
    for i = 1, 15 do
        local n = self._root:findChild("Panel/Panel_map/map_"..tostring(i))
        local render = BattleIconRender.create(n)
        render:onTownClicked(function(s, ti)
            -- dump(ti.config)
            -- dump(ti.data)
            if not ti then
                return
            end

            local bc = app:getInst("BiddingController")
            local townstatus = protobuf.enum_id("pb_town.PHASE", ti.data.phase)
            if townstatus == 0 then --未开放阶段
                bc:openTownInfoView()
                bc._townInfoView:refresh(ti)
            elseif townstatus == 1 then --竞价阶段
                if bc:hasPriceRight() and not bc:isMyTown(ti.data.id) then
                    bc:openPriceView()
                    bc._priceView:refresh(ti)
                else
                    bc:openTownInfoView()
                    bc._townInfoView:refresh(ti)
                end                
            elseif townstatus == 2 then --准备阶段
                bc:openEnterBattleView()
                bc._enterBattleView:refresh(ti)
            elseif townstatus == 3 then --开战阶段
                bc:openEnterBattleView()
                bc._enterBattleView:refresh(ti)
            elseif townstatus == 4 then --结束阶段
                bc:openTownInfoView()
                bc._townInfoView:refresh(ti)
            elseif townstatus == 5 then --易变阶段
                bc:openEnterBattleView()
                bc._enterBattleView:refresh(ti)
            end
            -- bc:addNotify()
        end)
        table.insert(self.icons, render)
    end    
end

function WorldBattleView:refresh(data)
    local hasTown = false
    for i = 1, 15 do
        self.icons[i]:setData(data.townInfos[i])
        if self.bc:isMyTown(data.townInfos[i].data.id ) then
            hasTown = true
        end
    end

    self.btnLook:setVisible(hasTown)

    if app.session:getVirtualTime() then
        self.year:setString(string.format(L("lua_code_text_399"), app.session:getVirtualTime().year))
    end
end

return WorldBattleView